package com.ieng.ormosia.web.controller.system;

import com.ieng.ormosia.common.exception.PrimaryKeyException;
import com.ieng.ormosia.common.utils.ActionR;
import com.ieng.ormosia.common.vo.ResponseData;
import com.ieng.ormosia.connect.model.Role;
import com.ieng.ormosia.connect.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/role")
public class RoleController {
	@Autowired
	private RoleService roleService;

	@Operation(summary = "角色列表")
	@RequestMapping("/list")
	public ResponseData list(){
		return ActionR.success(roleService.getRoleList());
	}

	@Operation(summary = "查询角色列表")
	@RequestMapping("/query")
	public ResponseData query(@RequestBody(required = false) Role role){
		return ActionR.success(roleService.getRoleHashQ(role));
	}

	@Operation(summary = "角色回收站")
	@RequestMapping(value = "/recycle")
	public ResponseData recycle(){
		return ActionR.success(roleService.getRoleDelAll());
	}

	@Operation(summary = "物理删除权限")
	@DeleteMapping(value = "/del")
	public ResponseData del(@RequestParam("id") Long id){
		roleService.delRole(id);
		return ActionR.send("删除成功");
	}

	@Operation(summary = "根据ID查询角色信息")
	@GetMapping
	public ResponseData get(@RequestParam("id") Long id){
		return ActionR.success(roleService.getRole(id));
	}

	@Operation(summary = "添加角色")
	@PostMapping
	public ResponseData add(@Validated @RequestBody Role role){
		roleService.addRole(role);
		return ActionR.send("新增角色");
	}

	@Operation(summary = "修改角色")
	@PostMapping
	public ResponseData edit(@RequestBody Role role){
		if(role.getId() == null){
			throw new PrimaryKeyException("role id is null");
		}
		roleService.editRole(role);
		return ActionR.send("修改成功");
	}

	@Operation(summary = "逻辑删除权限")
	@DeleteMapping
	public ResponseData remove(@RequestParam("id") Long id){
		roleService.removeRole(id);
		return ActionR.send("删除成功");
	}

}
